(() => {

    'use strict';
    goog.provide('Blockly.Arduino.ruilong');
    goog.require('Blockly.Arduino');

//机器人-电机转动
    Blockly.Arduino.forBlock['ruilong_293motor'] = function () {
        var id = Blockly.Arduino.valueToCode(this, 'ID', Blockly.Arduino.ORDER_ATOMIC);
        var speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        var code = 'motor(' + id + ',' + speed + ');\n';
        Blockly.Arduino.setups_['setup_output_4'] = 'pinMode(4, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_5'] = 'pinMode(5, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_6'] = 'pinMode(6, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_7'] = 'pinMode(7, OUTPUT);';


        var funcName = 'motor';
        var code2;
        code2 = 'void motor(int id,int speed){\n';
        code2 += 'int pin_s,pin_d;\n ';
        code2 += 'switch(id){\n';
        code2 += 'case 1:pin_d=4;pin_s=5;break;\n';
        code2 += 'case 2:pin_d=7;pin_s=6;break;\n';
        code2 += 'default:break;}\n';
        code2 += 'if(speed==0){digitalWrite(pin_d,0);digitalWrite(pin_s,0);}\n';
        code2 += 'if(speed>0){digitalWrite(pin_d,0);analogWrite(pin_s,speed);}\n';
        code2 += '       else{digitalWrite(pin_d,1);analogWrite(pin_s,speed);}\n';
        code2 += '}\n';

        Blockly.Arduino.definitions_[funcName] = code2;
        return code;
    };

    //机器人-电机转动
    Blockly.Arduino.forBlock['ruilong_8833motor'] = function () {
        var id = Blockly.Arduino.valueToCode(this, 'ID', Blockly.Arduino.ORDER_ATOMIC);
        var speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        var code = 'motor(' + id + ',' + speed + ');\n';
        Blockly.Arduino.setups_['setup_output_4'] = 'pinMode(4, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_5'] = 'pinMode(5, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_6'] = 'pinMode(6, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_7'] = 'pinMode(7, OUTPUT);';


        var funcName = 'motor';
        var code2;
        code2 = 'void motor(int id,int speed){\n';
        code2 += 'int pin_s,pin_d;\n '
        code2 += 'switch(id){\n';
        code2 += 'case 1:pin_d=4;pin_s=5;break;\n';
        code2 += 'case 2:pin_d=7;pin_s=6;break;\n';
        code2 += 'default:break;}\n';
        code2 += 'if(speed==0){digitalWrite(pin_d,0);digitalWrite(pin_s,0);}\n';
        code2 += 'if(speed>0){digitalWrite(pin_d,0);analogWrite(pin_s,speed);}\n';
        code2 += '       else{digitalWrite(pin_d,1);analogWrite(pin_s,(255+speed));}\n';
        code2 += '}\n';

        Blockly.Arduino.definitions_[funcName] = code2;
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_I2Cmotor'] = function () {
        var id = Blockly.Arduino.valueToCode(this, 'ID', Blockly.Arduino.ORDER_ATOMIC);
        var speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';

        Blockly.Arduino.definitions_['_Wire_h_'] = '#include <Wire.h> ';
        Blockly.Arduino.definitions_['_Adafruit_PWMServoDriver_h_'] = '#include <Adafruit_PWMServoDriver.h>';
        Blockly.Arduino.definitions_['_Adafruit_PWMServoDriver'] = 'Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();';



        Blockly.Arduino.setups_['setup_pwm_begin'] = 'pwm.begin();\n'
            + 'pwm.setOscillatorFrequency(27000000);\n'
            + 'pwm.setPWMFreq(400);\n'
            + 'Wire.setClock(400000);';

        var code2;
        code2 = 'void motor(int ID,int SPEED){  //0-7\n'
            + '    if(SPEED>0){pwm.setPin(ID*2, 0 );pwm.setPin(ID*2+1, (SPEED+1)*16-1);}\n'
            + '    else if(SPEED==0){pwm.setPin(ID*2, 4095 );pwm.setPin(ID*2+1, 4095);}\n'
            + '    else if(SPEED<0){pwm.setPin(ID*2, 1-(SPEED+1)*16);pwm.setPin(ID*2+1, 0);}\n'
            + '    }\n';

        Blockly.Arduino.definitions_['motor'] = code2;

        var code = 'motor(' + id + ',' + speed + ');\n';
        return code;
    };



Blockly.Arduino.forBlock['ESP32_Motor_Setup'] = function() {
   var id = Blockly.Arduino.valueToCode(this, 'MOTOR_ID', Blockly.Arduino.ORDER_ATOMIC);
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);
   var pinb = Blockly.Arduino.valueToCode(this, 'PINB', Blockly.Arduino.ORDER_ATOMIC);

    Blockly.Arduino.definitions_['RL_ESP32_Motor_h'] = '#include <RL_ESP32_Motor.h>';
    Blockly.Arduino.definitions_['Motor'+id] = 'Motor Esp32_Motor_' + id +'('+id+','+pina+','+pinb+');';
    Blockly.Arduino.setups_['Motor'+id+'_mcpwm_begin'] = 'Esp32_Motor_' + id +'.mcpwm_begin();';
   var code = '';
   return code;
 };

Blockly.Arduino.forBlock['ESP32_Motor_Speed'] = function() {
   var id = Blockly.Arduino.valueToCode(this, 'MOTOR_ID', Blockly.Arduino.ORDER_ATOMIC);
   var speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ATOMIC);

   var code = 'Esp32_Motor_' + id +'.Motor_Speed('+ speed +');\n';
   
   return code;
 };

 Blockly.Arduino.forBlock['ESP32_Encoder_getCountA']= function() {
   var enc_mode =  this.getFieldValue('ENC_MODE');
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);

    Blockly.Arduino.definitions_['#include_ESP32Encoder_h'] = '#include <ESP32Encoder.h>';
    Blockly.Arduino.definitions_['ESP32Encoder_encoder'+pina] = 'ESP32Encoder encoder_' +pina+';';
    Blockly.Arduino.setups_['ESP32Encoder_encoder'+pina+enc_mode] = 'encoder_' +pina+'.'+enc_mode+'('+pina+',-1);';
   var code = 'encoder_' +pina+'.getCount()';
   return [code, Blockly.Arduino.ORDER_ATOMIC];
 };
  Blockly.Arduino.forBlock['ESP32_Encoder_setCountA'] = function() {
  var enc_mode =  this.getFieldValue('ENC_MODE');
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);
   var val = Blockly.Arduino.valueToCode(this, 'VAL', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['#include_ESP32Encoder_h'] = '#include <ESP32Encoder.h>';
    Blockly.Arduino.definitions_['ESP32Encoder_encoder'+pina] = 'ESP32Encoder encoder_' +pina+';';
    Blockly.Arduino.setups_['ESP32Encoder_encoder'+pina+enc_mode] = 'encoder_' +pina+'.'+enc_mode+'('+pina+',-1);';
   var code = 'encoder_' +pina+'.setCount('+val+');\n';
   return code;
 };

 Blockly.Arduino.forBlock['ESP32_Encoder_crpCountA'] = function() {
   var enc_mode =  this.getFieldValue('ENC_MODE');
   var act =  this.getFieldValue('ACT');
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);

    Blockly.Arduino.definitions_['#include_ESP32Encoder_h'] = '#include <ESP32Encoder.h>';
    Blockly.Arduino.definitions_['ESP32Encoder_encoder'+pina] = 'ESP32Encoder encoder_' +pina+';';
    Blockly.Arduino.setups_['ESP32Encoder_encoder'+pina+enc_mode] = 'encoder_' +pina+'.'+enc_mode+'('+pina+',-1);';
   var code = 'encoder_' +pina+'.'+act+'();\n';
   return code;
 };

  Blockly.Arduino.forBlock['ESP32_Encoder_getCountAB'] = function() {
   var enc_mode =  this.getFieldValue('ENC_MODE');
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);
   var pinb = Blockly.Arduino.valueToCode(this, 'PINB', Blockly.Arduino.ORDER_ATOMIC);

    Blockly.Arduino.definitions_['#include_ESP32Encoder_h'] = '#include <ESP32Encoder.h>';
    Blockly.Arduino.definitions_['ESP32Encoder_encoder'+pina+pinb] = 'ESP32Encoder encoder_' +pina+pinb+';';
    Blockly.Arduino.setups_['ESP32Encoder_encoder'+pina+pinb+enc_mode] = 'encoder_' +pina+pinb+'.'+enc_mode+'('+pina+','+pinb+');';
   var code = 'encoder_' +pina+pinb+'.getCount()';
   return [code, Blockly.Arduino.ORDER_ATOMIC];
  };

Blockly.Arduino.forBlock['ESP32_Encoder_setCountAB'] = function() {
  var enc_mode =  this.getFieldValue('ENC_MODE');
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);
      var pinb = Blockly.Arduino.valueToCode(this, 'PINB', Blockly.Arduino.ORDER_ATOMIC);
   var val = Blockly.Arduino.valueToCode(this, 'VAL', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['#include_ESP32Encoder_h'] = '#include <ESP32Encoder.h>';
        Blockly.Arduino.definitions_['ESP32Encoder_encoder'+pina+pinb] = 'ESP32Encoder encoder_' +pina+pinb+';';
    Blockly.Arduino.setups_['ESP32Encoder_encoder'+pina+pinb+enc_mode] = 'encoder_' +pina+pinb+'.'+enc_mode+'('+pina+','+pinb+');';
   var code = 'encoder_' +pina+pinb+'.setCount('+val+');\n';
   return code;
 };

 Blockly.Arduino.forBlock['ESP32_Encoder_crpCountAB'] = function() {
   var enc_mode =  this.getFieldValue('ENC_MODE');
   var act =  this.getFieldValue('ACT');
   var pina = Blockly.Arduino.valueToCode(this, 'PINA', Blockly.Arduino.ORDER_ATOMIC);
   var pinb = Blockly.Arduino.valueToCode(this, 'PINB', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['#include_ESP32Encoder_h'] = '#include <ESP32Encoder.h>';
    Blockly.Arduino.definitions_['ESP32Encoder_encoder'+pina+pinb] = 'ESP32Encoder encoder_' +pina+pinb+';';
Blockly.Arduino.setups_['ESP32Encoder_encoder'+pina+pinb+enc_mode] = 'encoder_' +pina+pinb+'.'+enc_mode+'('+pina+','+pinb+');';
   var code = 'encoder_' +pina+pinb+'.'+act+'();\n';
   return code;
 };




    })();